﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace NewCapturerDemo
{
    public partial class FormPhotoPreviewing : Form
    {
        private Bitmap _bitmap = null;
        public FormPhotoPreviewing(Bitmap bitmap)
        {
            _bitmap = bitmap;
            InitializeComponent();          
        }

        private void FormPhotoPreviewing_Load(object sender, EventArgs e)
        {
            if (_bitmap != null)
            {
                Size size = _bitmap.Size;
                panel1.AutoScrollMinSize = size;
                pictureBox1.Size = size;
                pictureBox1.Location = new Point(0, 0);
                pictureBox1.Image = _bitmap;
                if(size.Width < panel1.Width && size.Height < panel1.Height)
                {
                    SetPictureBoxInCentre();
                }
            }
            buttonClose.Parent = this;
            int x = this.Width - buttonClose.Width;
            buttonClose.Location = new Point(x, 0);
            buttonClose.BackColor = Color.Transparent;
        }

        private void SetPictureBoxInCentre()
        {
            int x = (this.panel1.Width - pictureBox1.Width) / 2;
            int y = (this.panel1.Height - pictureBox1.Height) / 2;
            pictureBox1.Location = new Point(x, y);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveFile()
        {
            SaveFileDialog form = new SaveFileDialog();
            form.InitialDirectory = "D:\\out";
            form.Filter = "JPG图片(*.jpg)|*.jpg|BMP图片(*.bmp)|*.bmp|PNG图片(*.png)|*.png";
            form.FileOk += new CancelEventHandler(form_FileOk);
            form.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = form.FileName;
                int index = form.FilterIndex;
                ImageFormat formats = ImageFormat.Jpeg;
                switch (index)
                {
                    case 1:
                        formats = ImageFormat.Jpeg;
                        break;
                    case 2:
                        formats = ImageFormat.Bmp;
                        break;
                    case 3:
                        formats = ImageFormat.Png;
                        break;
                    default:
                        break;
                }
                try
                {
                    _bitmap.Save(fileName, formats);
                    MessageBox.Show("文件保存成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存照片发生了错误:" + ex.Message);
                }
            }
        }

        private void 保存照片ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            SaveFile();
        }

        private void form_FileOk(object sender, CancelEventArgs e)
        {
            
        }

        private void buttonSaveFile_Click(object sender, EventArgs e)
        {
            SaveFile();
        }
    }
}
